package com.oasistudio.ads.expert.banner;

import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.adwo.adsdk.AdListener;
import com.adwo.adsdk.AdwoAdView;
import com.adwo.adsdk.ErrorCode;
import com.oasistudio.ads.expert.R;

public class AdViewAdwo {
	// String Adwo_PID =
	// "2b8dbd92edd74a97b3ba6b0189bef125";//Adwo官方Sample中的Adwo_pid
	// 填写在Adwo官网上为你程序申请的Adwo_PID
	public static final String Adwo_PID = "f210758617994dfea3f21c8dd92d4359";
	public static final String UNION = "adwo.com";
	AdwoAdRequestListener adRequestListener;
	Activity activity;

	public AdViewAdwo(Activity activity) {
		this.activity = activity;
		this.adRequestListener = new AdwoAdRequestListener(activity, UNION);

		addAdwoAd();
	}

	/************************** www.adwo.com start *************************/
	private void addAdwoAd() {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		LinearLayout adwoContainer = new LinearLayout(activity);
		adContainer.addView(adwoContainer);

		AdwoAdView adwoAdView = new AdwoAdView(activity, // Activity 或 Context
				Adwo_PID, // 填写在 Adwo 官网上为你程序申请的 Adwo_PID
				false, // 标识 Banner 广告测试模式是否开启 true 为测试模式
				30); // Banner 广告刷新请求时间间隔
		// doesn't work
		// adwoAdView.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// System.out.println("@@@@@@@............... adwoAdView.onClick().................@@@@@@@@@@");
		// }
		// });
		// adwoAdView.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// System.out.println("@@@@@@@............... adwoAdView.onClick2().................@@@@@@@@@@");
		// }
		// });
		adwoAdView.setListener(this.adRequestListener);
		adwoContainer.addView(adwoAdView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	/************************** www.adwo.com end *************************/
	class AdwoAdRequestListener extends AbstractAdsExpertAdListener implements AdListener {

		public AdwoAdRequestListener(Activity activity, String union) {
			super(activity, union);
		}

		@Override
		public void onFailedToReceiveAd(AdwoAdView arg0) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onFailedToReceiveAd(AdwoAdView arg0, ErrorCode arg1) {
			// TODO Auto-generated method stub
			this.onFailedToReceiveAd(arg0);
		}

		@Override
		public void onFailedToReceiveRefreshedAd(AdwoAdView arg0) {
			// TODO Auto-generated method stub
			this.onFailedToReceiveAd(arg0);
		}
		@Override
		public void onReceiveAd(AdwoAdView adView) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}
	}
}
